/*     */ package com.liy.framework.common.redis.config;
/*     */ 
/*     */ import com.fasterxml.jackson.annotation.JsonAutoDetect;
/*     */ import com.fasterxml.jackson.annotation.PropertyAccessor;
/*     */ import com.fasterxml.jackson.databind.ObjectMapper;
/*     */ import java.lang.reflect.Method;
/*     */ import java.time.Duration;
/*     */ import org.springframework.cache.CacheManager;
/*     */ import org.springframework.cache.annotation.CachingConfigurerSupport;
/*     */ import org.springframework.cache.annotation.EnableCaching;
/*     */ import org.springframework.cache.interceptor.KeyGenerator;
/*     */ import org.springframework.context.annotation.Bean;
/*     */ import org.springframework.context.annotation.Configuration;
/*     */ import org.springframework.data.redis.cache.RedisCacheConfiguration;
/*     */ import org.springframework.data.redis.cache.RedisCacheManager;
/*     */ import org.springframework.data.redis.connection.RedisConnectionFactory;
/*     */ import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
/*     */ import org.springframework.data.redis.core.RedisTemplate;
/*     */ import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
/*     */ import org.springframework.data.redis.serializer.RedisSerializationContext;
/*     */ import org.springframework.data.redis.serializer.StringRedisSerializer;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ @Configuration
/*     */ @EnableCaching
/*     */ public class RedisCacheConfig
/*     */   extends CachingConfigurerSupport
/*     */ {
/*     */   @Bean
/*     */   public KeyGenerator keyGenerator() {
/*  54 */     return new KeyGenerator()
/*     */       {
/*     */         public Object generate(Object target, Method method, Object... params) {
/*  57 */           StringBuilder sb = new StringBuilder();
/*  58 */           sb.append(target.getClass().getName());
/*  59 */           sb.append(method.getName());
/*  60 */           for (Object obj : params) {
/*  61 */             sb.append(obj.toString());
/*     */           }
/*  63 */           System.out.println("cache key = " + sb.toString());
/*  64 */           return sb.toString();
/*     */         }
/*     */       };
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @Bean
/*     */   public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
/* 102 */     RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
/* 103 */     redisCacheConfiguration.entryTtl(Duration.ofSeconds(60L));
/*     */     
/* 105 */     StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
/* 106 */     Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
/*     */ 
/*     */     
/* 109 */     redisCacheConfiguration.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer));
/* 110 */     redisCacheConfiguration.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));
/*     */     
/* 112 */     return RedisCacheManager.builder(connectionFactory).cacheDefaults(redisCacheConfiguration).build();
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   @Bean
/*     */   public RedisTemplate<String, Object> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
/* 139 */     StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
/*     */     
/* 141 */     Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
/*     */ 
/*     */     
/* 144 */     ObjectMapper om = new ObjectMapper();
/* 145 */     om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
/* 146 */     om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
/* 147 */     jackson2JsonRedisSerializer.setObjectMapper(om);
/*     */     
/* 149 */     RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
/*     */     
/* 151 */     template.setKeySerializer(stringRedisSerializer);
/* 152 */     template.setValueSerializer(jackson2JsonRedisSerializer);
/*     */     
/* 154 */     template.setHashKeySerializer(stringRedisSerializer);
/* 155 */     template.setHashValueSerializer(jackson2JsonRedisSerializer);
/*     */     
/* 157 */     template.setConnectionFactory(redisConnectionFactory);
/* 158 */     template.afterPropertiesSet();
/* 159 */     return template;
/*     */   }
/*     */ }


/* Location:              C:\Users\admin\Desktop\Jar\das-framework-common-redis-1.0.0-SNAPSHOT.jar!\com\das\framework\common\redis\config\RedisCacheConfig.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.0.7
 */